package com.example.word.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.dict.R;
import com.example.word.bean.Dict;
import com.example.word.bean.StudyPlan;
import com.example.word.bean.User;
import com.example.word.bean.Word;
import com.example.word.ui.fragment.HomeFragment;
import com.example.word.util.CommonUtils;
import com.example.word.util.HttpUtils;
import com.example.word.util.MessageUtils;
import com.example.word.util.Response;
import com.example.word.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * 欢迎页面
 */
public class OpeningActivity extends AppCompatActivity {
    private Activity myActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myActivity = this;
        //设置页面布局
        setContentView(R.layout.activity_opening);
        initView();
    }

    private void initView() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
                    finish();
                    return;
                }
                Boolean isFirst = (Boolean) SPUtils.get(myActivity, SPUtils.IF_FIRST, true);
                String account = (String) SPUtils.get(myActivity, SPUtils.ACCOUNT, "");
                HttpUtils.sendGetRequest("/getServerUrl", new HashMap<>(), myActivity, Object.class, new HttpUtils.ApiCallback<Object>() {
                    @Override
                    public void onSuccess(Response<Object> response) {
                        // 跳转到主页面
                        Intent intent = new Intent();
                        intent.setClass(OpeningActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    @Override
                    public void onFailure(Response<Object> response, Context activity) {
                        HttpUtils.sendGetRequestStringResponse("https://fccs-dsu.oss-cn-beijing.aliyuncs.com/dic/cpolar-server-url.txt?OSSAccessKeyId=LTAI4Fynb8gTQzm8hsZnYmBL&Expires=2223384874&Signature=yg%2BmxNrFRhnd9bPa4wdUfIVEGcU%3D", new HashMap<>(), responseString -> {
                            HttpUtils.serverUrl = responseString;
                            HttpUtils.downloadUrl = HttpUtils.serverUrl + HttpUtils.projectName + "/download?fileName=";
                            // 跳转到主页面
                            Intent intent = new Intent();
                            intent.setClass(OpeningActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        });
                    }
                });

                if (isFirst) {
                    //第一次进来  初始化本地数据
                    SPUtils.put(myActivity, SPUtils.IF_FIRST, false);//第一次
                }
            }
        }, 1500);
    }

    @Override
    public void onBackPressed() {

    }
}
